package xFGE.Loader
{

	import flash.events.*;
	import flash.net.URLRequest;
	import flash.utils.*;
	
	//[Event(name="onComplete",type="flash.events.Complete")]
	/**
	 * 加载器基类，提供加载器通用方法
	 * @author Ben
	 * @version 1.0
	 * @updated 27-三月-2009 11:24:47
	 */
	public class LoaderBase extends EventDispatcher
	{
		
		/** 加载器状态 */
		protected var _loaderStatus:String;
		
		/** http状态 */
		protected var _httpTatus:int;
		
		/** 出错信息 */
		protected var _errorText:String;
		
		/** 当前URL请求 */
		protected var _url:URLRequest;
		
		/** 加载器类型	*/
		protected var _strLoaderType:String;
		
		/** 加入时间 */
		protected var _strAddTime:String;
		
		/** 最后记录时间 */
		protected var _lastTime:int;
		
		/** 载入时间 */
		protected var _loadedTime:Number;
		
		/** 最后速度检测时间 */
		protected var _lastCheckSpeedTime:int;
		
		/** 载入即时速度 */
		protected var _loadedSpeed:int;
		
		/** 平均速度 */
		protected var _loadedAvgSpeed:int;
		
		/** 响应时间 */
		protected var _responseTime:int;
		
		/** 最后已载入字节数 */
		protected var _lastLoadedBytes:int;
		
		/** 已经加载的字节数*/
		protected var _loadedBytes:int;
		
		/** 一共要载入的字节数 */
		protected var _totalLoadedBytes:int;
		
		/** 已加载进度百分比 */
		protected var _loadedPrecent:Number;
		
		/** 加载的数据 */
		protected var _content:*;
		
		/** 加载信息 */
		protected var _resourceObj:ResourceObj;
		
		
		public function LoaderBase(strLoaderType:String)
		{
			//TODO: implement function
			_strLoaderType = strLoaderType;
			_loaderStatus = xLoader.LOADER_STATUS_IDLE;
			
		}
		// 单个资源载入前处理
		protected function onStart(eveObj:Event):void
		{
			//初始化数据
			_lastCheckSpeedTime = getTimer();
			_lastLoadedBytes = 0;
			// 获取当前时间
			var Now:Date = new Date();
			_strAddTime = Now.fullYear+"年"+(Now.month+1)+"月"+Now.date+"日"+" "+Now.hours+":"
							+ Now.minutes +":" + Now.seconds;
			_responseTime = getTimer() - _lastTime;
			_lastTime += _responseTime;
			
			trace(_strAddTime+ " "+"响应时间:"+_responseTime+"ms"+"开始加载:"+_url.url);
			//加载器状态
			_loaderStatus = xLoader.LOADER_STATUS_BUSY;
			dispatchEvent(eveObj);
			return;
		}
		// 单个资源载入完成处理
		protected  function onComplete(eveObj:Event):void
		{
			// 加载完成，加载器完成状态
			_loaderStatus = xLoader.LOADER_STATUS_COMPLETE;
			
			_loadedTime = (getTimer() - _lastTime)/1000;
			_loadedAvgSpeed = (_loadedBytes/1024)/_loadedTime;
			trace("完成"+_resourceObj.name+"加载"+" " + "耗时(s):" + _loadedTime + "s" + "平均速度(k/s):"+ _loadedAvgSpeed);
			//_content = 
			dispatchEvent(eveObj);
			return;
		}
		
		// 单个资源载入过称处理
		 protected  function onProgress(eveObj:*):void
		 {
		 	//更新信息
		 	_loadedBytes = eveObj.bytesLoaded;
		 	_totalLoadedBytes = eveObj.bytesTotal;
		 	_loadedPrecent =xLoader.truncateNumber(( _loadedBytes / _totalLoadedBytes ),3);
		 	
		 	//计算速度
		 	_lastCheckSpeedTime = getTimer()-_lastCheckSpeedTime;
		 	var deltaTime:Number = _lastCheckSpeedTime / 1000;
		 	var deltaKBytes:Number = (eveObj.bytesLoaded - _lastLoadedBytes)/1024;
		 	if(deltaTime == 0 && deltaKBytes == 0) _loadedSpeed = 0;
		 	else _loadedSpeed = deltaKBytes / deltaTime;
		 	
			_resourceObj.loadedPresent = _loadedPrecent;
			_resourceObj.loadedSpeed = _loadedSpeed;
		 	//_loadedSpeed = (eveObj.bytesLoaded - _lastLoaedBytes)/(getTimer()-	
		 	//trace("加载"+_resourceObj.name+"进度："+_loadedPrecent +"%" + "速度(k/s):"+ _loadedSpeed);
		 	dispatchEvent(eveObj);
		 	return;
		 }
		 
		 // 网络出链接状态处理
		 protected  function onHttpStatus(eveObj:HTTPStatusEvent):void
		 {
		 	//trace("http句柄："+eveObj);
		 	
		 	if(eveObj.status == 404)
		 	{
		 		//trace("网络无连接或连接无效");
		 		dispatchEvent(eveObj);
		 	}
		 	return;
		 }
		 
		 // IO错误处理
		protected  function onIOError(eveObj:IOErrorEvent):void
		 {
		 	//trace("资源文件不存在，请与管理员联系");
		 	dispatchEvent(eveObj);
		 	return;
		 }
		// 沙窗安全错误处理
		protected function onSecurityErrorHandler(e : ErrorEvent) : void
		{
        	dispatchEvent(new ErrorEvent(e.text));
        }
		/**
		 * 加载资源
		 * @param strFile 文件资源路径
		 * 
		 */
		public function Load(resourceObj:ResourceObj):void
		{
			_resourceObj = resourceObj; 
			_loaderStatus = xLoader.LOADER_STATUS_BUSY;
			_url = _resourceObj.url;
			_lastTime = getTimer();
			return;
		}
		
		/**
		 * 移除监loader
		 * 
		 */
		public function Clear():void{
			return;
		}
		
		public function get strLoaderType():String
		{
			return _strLoaderType;
		}
		
		
		public function get loadedBytes():int
		{
			return _loadedBytes;
		}
		
		public function get content():*
		{
			return _content;
		}
		
		public function get loaderStatus():String
		{
			return _loaderStatus;
		}
		
		public function set loaderStatus(status:String):void
		{
			_loaderStatus = status;
		}
		
		public function get resourceObj():ResourceObj
		{
			return _resourceObj;
		}
		
		public function get loadedPrecent():int
		{
			return _loadedPrecent;
		}
	}
	
}